package com.hanhai.material.device.controller;

import com.hanhai.material.common.result.Result;
import com.hanhai.material.common.result.ResultTool;
import com.hanhai.material.device.entity.SysProductRecordDetention;
import com.hanhai.material.device.entity.SysProductRecordDetentionRecord;
import com.hanhai.material.device.mapper.SysProductRecordDetentionMapper;
import com.hanhai.material.device.service.SysProductRecordDetentionRecordService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.UUID;

import static com.hanhai.material.device.entity.table.SysProductRecordDetentionRecordTableDef.SYS_PRODUCT_RECORD_DETENTION_RECORD;

/**
 * 控制层。
 *
 * @author youchuang
 * @since 2024/01/30 14:07:20
 */
@RestController
@RequestMapping("/sysProductRecordDetentionRecord")
public class SysProductRecordDetentionRecordController {

    @Autowired
    private SysProductRecordDetentionRecordService sysProductRecordDetentionRecordService;

    @Autowired
    private SysProductRecordDetentionMapper sysProductRecordDetentionMapper;

    /**
     * 添加。
     *
     * @param sysProductRecordDetentionRecord
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    public Result<?> save(@RequestBody SysProductRecordDetentionRecord sysProductRecordDetentionRecord) {
        sysProductRecordDetentionRecord.setId(UUID.randomUUID().toString());
        String num = "num-" + sysProductRecordDetentionRecord.getNum();
        boolean result = UpdateChain.of(SysProductRecordDetention.class)
                .setRaw(SysProductRecordDetention::getNum, num)
                .where(SysProductRecordDetention::getId).eq(sysProductRecordDetentionRecord.getProductRecordDetentionId())
                .update();
        return sysProductRecordDetentionRecordService.save(sysProductRecordDetentionRecord) ? ResultTool.success() : ResultTool.fail();
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return sysProductRecordDetentionRecordService.removeById(id);
    }

    @PostMapping("/remove")
    public Result<?> remove(@RequestBody SysProductRecordDetentionRecord sysProductRecordDetentionRecord) {
        String num = "num+" + sysProductRecordDetentionRecord.getNum();
        boolean result = UpdateChain.of(SysProductRecordDetention.class)
                .setRaw(SysProductRecordDetention::getNum, num)
                .where(SysProductRecordDetention::getId).eq(sysProductRecordDetentionRecord.getProductRecordDetentionId())
                .update();
        result = this.sysProductRecordDetentionRecordService.removeById(sysProductRecordDetentionRecord.getId());
        return result ? ResultTool.success() : ResultTool.fail();
    }

    /**
     * 根据主键更新。
     *
     * @param sysProductRecordDetentionRecord
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    public boolean update(@RequestBody SysProductRecordDetentionRecord sysProductRecordDetentionRecord) {
        return sysProductRecordDetentionRecordService.updateById(sysProductRecordDetentionRecord);
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("listByIntoId/{productRecordIntoId}")
    public Result<?> listByIntoId(@PathVariable String productRecordIntoId) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION_RECORD.PRODUCT_RECORD_INTO_ID.eq(productRecordIntoId));
        List<SysProductRecordDetentionRecord> sysProductRecordDetentionRecordList = sysProductRecordDetentionRecordService.list(queryWrapper);
        return CollectionUtils.isEmpty(sysProductRecordDetentionRecordList) ? ResultTool.fail() : ResultTool.success(sysProductRecordDetentionRecordList);

    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("listByShiftId/{productRecordShiftId}")
    public Result<?> listByShiftId(@PathVariable String productRecordShiftId) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.where(SYS_PRODUCT_RECORD_DETENTION_RECORD.PRODUCT_RECORD_SHIFT_ID.eq(productRecordShiftId));
        List<SysProductRecordDetentionRecord> sysProductRecordDetentionRecordList = sysProductRecordDetentionRecordService.list(queryWrapper);
        return CollectionUtils.isEmpty(sysProductRecordDetentionRecordList) ? ResultTool.fail() : ResultTool.success(sysProductRecordDetentionRecordList);

    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    public SysProductRecordDetentionRecord getInfo(@PathVariable Serializable id) {
        return sysProductRecordDetentionRecordService.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<SysProductRecordDetentionRecord> page(Page<SysProductRecordDetentionRecord> page) {
        return sysProductRecordDetentionRecordService.page(page);
    }

}
